package club.ljwang.interceptor;

import club.ljwang.utils.JedisUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

import javax.servlet.http.HttpServletRequest;

/**
 * @description:
 * @author: ming
 * @date: 2020/3/8-21:08
 * @Version: 1.0.0
 */

public class MermberMessageResovle implements HandlerMethodArgumentResolver {

    @Autowired
    private JedisUtil jedisUtil;
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        //看你的方法是否有Member注解
        return methodParameter.hasParameterAnnotation(Member.class);
    }

    @Override
    public Object resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        //从redis获取用户信息
        HttpServletRequest request = nativeWebRequest.getNativeRequest(HttpServletRequest.class);
        String token = request.getHeader("token");
        String s = jedisUtil.get(token);
        //获取对象直接返回，会复制给你加注解的参数
        return JSON.parseObject(s, club.ljwang.pojo.Member.class);
    }
}
